import BaseScene from "./core/BaseScene";
import EventManager from "./core/EventManager";
import SceneNavigator from "./core/SceneNavigator";
import { UIManager } from "./core/UIManager";
import HotUpdateModule from "./module/HotUpdateModule";
import { NetInfo } from "./net/Commond";
import { CommonEvents } from "./net/CommonEvents";
import { HttpRequest } from "./net/HttpRequest";
import LogUtil from "./utils/LogUtil";
import MathUtil from "./utils/MathUtil";

const { ccclass, property } = cc._decorator;

@ccclass
export default class loadScene extends BaseScene {

    @property({ type: cc.Node, tooltip: CC_DEV && '更新节点' })
    public updateNode: cc.Node = null;

    @property({ type: cc.Label, tooltip: CC_DEV && '文本进度' })
    public labProgress: cc.Label = null;

    @property({ type: cc.ProgressBar, tooltip: CC_DEV && '进度条进度' })
    public progressBar: cc.ProgressBar = null;

    @property({ type: cc.Asset, tooltip: CC_DEV && 'manifest文件' })
    localManifestUrl: cc.Asset = null;

    @property({ type: cc.Label, tooltip: CC_DEV && '版本号' })
    labVersion: cc.Label = null;

    @property({ type: cc.Label, tooltip: CC_DEV && '更新说明' })
    labInfo: cc.Label = null;

    onLoad(): void {
        super.onLoad();
        SceneNavigator.setHome('mainScene');
        this.setLabVersion();
    }

    private setLabVersion(): void {
        this.labVersion.string = HotUpdateModule.ins().getVersion();
    }

    protected start(): void {
        this.updateNode.active = false;
        this.checkHotUpdate();
    }

    onEnable() {
        EventManager.on(CommonEvents.HOTUPDATE_FINISH, this.onHotUpdateFinish, this);
        EventManager.on(CommonEvents.HOTUPDATE_RATE, this.onHotUpdateRate, this);
        EventManager.on(CommonEvents.HOTUPDATE_CANCEL, this.HotUpdateCancel, this);
    }

    onDisable() {
        EventManager.off(CommonEvents.HOTUPDATE_FINISH, this.onHotUpdateFinish, this);
        EventManager.off(CommonEvents.HOTUPDATE_RATE, this.onHotUpdateRate, this);
        EventManager.off(CommonEvents.HOTUPDATE_CANCEL, this.HotUpdateCancel, this);
    }

    onDestroy(): void {
        super.onDestroy();
        HotUpdateModule.ins().clear();
    }

    //热更新结束
    onHotUpdateFinish(param) {
        LogUtil.log(LogUtil.getFileName(),'热更新结束>>>', param);
        this.updateNode.active = false;
    }

    //热更进度
    onHotUpdateRate(param: number) {
        LogUtil.log(LogUtil.getFileName(),'热更进度>>>', param);
        let percent = param;
        if (percent > 1) {
            percent = 1;
        }

        let _progress = MathUtil.getProgress(percent);
        this.labProgress.string = _progress + '%';
    }

    HotUpdateCancel() {
        this.updateNode.active = false;
    }

    //前往大厅
    onBtnGoHome() {
        SceneNavigator.goHome({});
    }

    //检查版本更新
    onBtnCheckUpdate() {
        HotUpdateModule.ins().checkUpdate();
    }

    onBtnClose() {
        this.updateNode.active = false;
    }

    checkHotUpdate() {
        this.updateNode.active = false;
        HotUpdateModule.ins().loadManifest('project', (version: string) => {
            HotUpdateModule.ins().setVersion(version);
            this.setLabVersion();
            if (!cc.sys.isNative) {
                return;
            }
            UIManager.ins().showUILoading(0);
            let httpPromise = HttpRequest.ins().httpPromise(`http://${NetInfo.ip}:${NetInfo.dwport}/downloads/base-update/version.manifest`, {}, 'get', () => {
                UIManager.ins().hideUILoading();
            });
            httpPromise.then((data: any) => {
                UIManager.ins().hideUILoading();
                LogUtil.log(LogUtil.getFileName(),`serverVersion:${data.version},localVersion:${version}`);
                if (MathUtil.versionCompare(data.version, version)) {
                    this.updateNode.active = true;
                    this.labInfo.string = `发现新版本:${data.version}`;
                    HotUpdateModule.ins().setVersion(data.version);
                }
            }).catch(() => {
                UIManager.ins().hideUILoading();
            });
        });
    }
}